package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Api(tags = "套餐接口")
@RequestMapping("/admin/setmeal")
public class SetMealController {
    @Autowired
    private SetMealService setMealService;

    // 分页查询套餐
    @ApiOperation("分页查询套餐")
    @GetMapping("/page")
    public Result<PageResult> selectSetMealList(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageResult pageResult = setMealService.selectSetMealList(setmealPageQueryDTO);

        return Result.success(pageResult);
    }

    // 新增套餐
    @ApiOperation("新增套餐接口")
    @PostMapping
    @CacheEvict(cacheNames = "setMeal", key = "#setmealDTO.categoryId")
    public Result<String> addSetMeal(@RequestBody SetmealDTO setmealDTO) {
        setMealService.addSetMeal(setmealDTO);

        return Result.success();
    }

    // 套餐启售/禁售
    @ApiOperation("套餐启售和禁售")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setMeal", allEntries = true)
    public Result<String> changeStatus(@PathVariable Integer status, @RequestParam Long id) {
        setMealService.changeStatus(status, id);

        return Result.success();
    }

    // id获取套餐信息
    @ApiOperation("id获取套餐信息")
    @GetMapping("/{id}")
    public Result<SetmealVO> getSetMealById(@PathVariable Long id) {
        SetmealVO setmealVO = setMealService.getSetMealById(id);

        return Result.success(setmealVO);
    }

    // 修改套餐信息
    @ApiOperation("修改套餐信息")
    @PutMapping
    @CacheEvict(cacheNames = "setMeal", allEntries = true)
    public Result<String> updateSetMealInfo(@RequestBody SetmealDTO setmealDTO) {
        setMealService.updateSetMealInfo(setmealDTO);

        return Result.success();
    }

    // 删除套餐
    @ApiOperation("删除套餐信息")
    @DeleteMapping
    @CacheEvict(cacheNames = "setMeal", allEntries = true)
    public Result<String> deleteSetMeal(@RequestParam List<Long> ids) {
        setMealService.deleteSetMeal(ids);

        return Result.success();
    }
}
